package com.yungu.swift.admin.route.finance;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.utils.ExcelUtil;
import com.yungu.swift.assets.passenger.model.dto.PassengerInvoiceLogDto;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerInvoicePageParam;
import com.yungu.swift.assets.passenger.model.param.AdminPassengerInvoiceParam;
import com.yungu.swift.assets.passenger.model.vo.PassengerInvoiceAdminVo;
import com.yungu.swift.assets.passenger.service.PassengerInvoiceLogService;
import com.yungu.swift.assets.passenger.service.PassengerInvoiceService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.order.model.param.AdminOrderInvoicePageParam;
import com.yungu.swift.order.model.vo.AdminOrderInvoiceDetailVo;
import com.yungu.swift.order.service.OrderAdminService;
import com.yungu.swift.system.finance.model.dto.FinanceActualFlowDto;
import com.yungu.swift.system.finance.model.param.AdminRechargeInvoicePageParam;
import com.yungu.swift.system.finance.service.FinanceActualFlowService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.yungu.swift.admin.consts.AdminErrorEnum.PARAM_ERROR;
import static com.yungu.swift.constants.PassengerConstant.INVOICE_BILLING;
import static com.yungu.swift.constants.PassengerConstant.INVOICE_SEND;

/**
 * @author : cuixiuyin
 * @date : 2019/8/16
 */
@RestController
@RequestMapping("/admin/v1/finance/invoice")
@Api(value = "【财务管理】", tags = "发票管理")
public class InvoiceAdmin {

    @Reference
    private PassengerInvoiceService passengerInvoiceService;
    @Reference
    private PassengerInvoiceLogService passengerInvoiceLogService;
    @Reference
    private OrderAdminService orderAdminService;
    @Reference
    private FinanceActualFlowService financeActualFlowService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "发票管理分页查询", httpMethod = "POST")
    public ResponseData<PageVo<PassengerInvoiceAdminVo>> queryPage(@RequestBody AdminPassengerInvoicePageParam invoicePageParam) {
        if (ParamUtil.isIllegal(invoicePageParam.getStatus())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return passengerInvoiceService.queryPageAdmin(invoicePageParam);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "发票管理信息导出", httpMethod = "POST")
    public void export(HttpServletResponse response, @RequestBody AdminPassengerInvoicePageParam invoicePageParam) {
        if (ParamUtil.isIllegal(invoicePageParam.getStatus())) {
            return;
        }
        if (INVOICE_BILLING != invoicePageParam.getStatus() || INVOICE_SEND != invoicePageParam.getStatus()) {
            return;
        }
        String templatePath = null;
        String fileName = ExcelUtil.getFileName("发票");
        //待寄出
        if (INVOICE_BILLING == invoicePageParam.getStatus()) {
            templatePath = "/template/invoiceWaiteToBeSendOut.xlsx";
        }
        //已寄出
        if (INVOICE_SEND == invoicePageParam.getStatus()) {
            templatePath = "/template/invoiceSent.xlsx";
        }
        List<PassengerInvoiceAdminVo> list = passengerInvoiceService.listExport(invoicePageParam).getData();
        ExcelUtil.export(response, list, PassengerInvoiceAdminVo.class, templatePath, fileName, null,
                "yyy/MM/dd hh:mm:ss");
    }


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "发票详情", httpMethod = "GET")
    public ResponseData<PassengerInvoiceAdminVo> detail(@RequestParam String uuid) {
        return passengerInvoiceService.detail(uuid);
    }

    @RequestMapping(value = "/trace", method = RequestMethod.GET)
    @ApiOperation(value = "开票轨迹", httpMethod = "GET")
    public ResponseData<List<PassengerInvoiceLogDto>> trace(@RequestParam String uuid) {
        return passengerInvoiceLogService.list(MapUtils.build("passengerInvoiceUuid", uuid));
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "发票信息新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminPassengerInvoiceParam passengerInvoiceParam) {
        if (ParamUtil.isIllegal(passengerInvoiceParam.getUuid(), passengerInvoiceParam.getStatus(),
                passengerInvoiceParam.getType(), passengerInvoiceParam.getOrderUuid())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return passengerInvoiceService.saveOrUpdate(passengerInvoiceParam);
    }

    @RequestMapping(value = "/tripPage", method = RequestMethod.POST)
    @ApiOperation(value = "按行程开票分页信息", httpMethod = "POST")
    public ResponseData<PageVo<AdminOrderInvoiceDetailVo>> tripPage(@RequestBody AdminOrderInvoicePageParam invoicePageParam) {
        if (ParamUtil.isIllegal(invoicePageParam.getOrderUuids())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return orderAdminService.orderInvoicePage(invoicePageParam);
    }

    @RequestMapping(value = "/rechargePage", method = RequestMethod.POST)
    @ApiOperation(value = "按充值开票分页信息", httpMethod = "POST")
    public ResponseData<PageVo<FinanceActualFlowDto>> rechargePage(@RequestBody AdminRechargeInvoicePageParam invoicePageParam) {
        if (ParamUtil.isIllegal(invoicePageParam.getOrderUuids())) {
            return ResponseData.buildErrorResponse(PARAM_ERROR.getCode(), PARAM_ERROR.getMsg());
        }
        return financeActualFlowService.rechargeInvoicePage(invoicePageParam);
    }

}
